﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.SessionState;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class Search2 : System.Web.UI.Page
    {
        ShopingCart vpp = new ShopingCart();
        public string tk = "";
        public static int loai = 0;
        public static int nh = 0;
        protected void Page_Load(object sender, EventArgs e)
        {  
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["TagItem"]))
                {
                    //string key = Request.QueryString["TagItem"].Replace(".aspx", "");
                    string key = Request.QueryString["TagItem"];
                    DataTable dt = new cmsSanPhamBL().SelectbyTagItem(key);
                    bindatalist(dt);
                }

                MasterPage mt = this.Master as MasterPage;
                Control uc = mt.FindControl("UcDanhMuc1") as Control;
                uc.Visible = true;
                Control uc2 = mt.FindControl("UcDanhMuc2") as Control;
                uc2.Visible = false;

                loadLoai();
                loadTH();
                loadNews();
            }
        }
        public void loadLoai()
        {
            drp_loai.DataSource = new cmsLoaiSanPhamBL().SelectAll(0);
            drp_loai.DataTextField = "TenLoaiSanPham";
            drp_loai.DataValueField = "LoaiSanPhamID";
            drp_loai.DataBind();
          //  drp_loai.Items.Add("Tất cả");
        }
        public void loadTH()
        {
            drp_TH.DataSource = new cmsNhanHieuBL().SelectAll();
            drp_TH.DataTextField = "TenNhanHieu";
            drp_TH.DataValueField = "NhanHieuID";
            drp_TH.DataBind();
      //      drp_TH.Items.Add("Tất cả");
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 8;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                lbl.Text = "";
                rpt.Visible = true;
                rpt.DataSource = objPds;
                rpt.DataBind();
            }
            else
            {
                rpt.Visible = false;
                lbl.Text = "Không tìm thấy \"" + Request.QueryString["ID"].ToString() + "\"";
                //drp_loai.Visible = false;
                //drp_TH.Visible = false;
            }
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            loadNews();
        }
        private void loadNews()
        {
            if (Request.QueryString["ID"] != null)
            {
                tk = Request.QueryString["ID"].ToString();
                tk.Trim();
                string loai2 = "";
                string nhanhieu = "";
                if(drp_loai.SelectedIndex != 0)
                    loai2 = "<Span style='color:Black;'> - Loại </span>\"" + drp_loai.SelectedItem.Text + "\"";
                if(drp_TH.SelectedIndex != 0)
                    nhanhieu = "<Span style='color:Black;'> - Thương hiệu: </span>\"" + drp_TH.SelectedItem.Text + "\"";
                lbtk.Text = "<Span style='color:Black;'>Kết quả tìm kiếm sản phẩm </span>\"" + tk + "\"" + loai2 + nhanhieu;
                Page.Title = tk;
                bindatalist(new cmsSanPhamBL().TimKiem(tk, loai, nh));
            }

        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            loadNews();
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        public cmsGioHangDO init()
        {
            cmsGioHangDO obj = new cmsGioHangDO();
            if (Session["ClientLoginID"] == null)
                obj.ClientLoginID = 0;
            else
                obj.ClientLoginID = int.Parse(Session["ClientLoginID"].ToString());
            obj.NgayDatHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.NgayGiaoHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.SoLuong = 1;
            obj.XuLy = false;
            return obj;
        }
        protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string url = Request.Url.AbsolutePath;
            try
            {
                HttpSessionState ss = HttpContext.Current.Session;
                string session = ss.SessionID;
                Int64 id = Int64.Parse(e.CommandArgument.ToString());
                cmsSanPhamDO obj1 = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = id });
                float gia = (float)(obj1.DonGia - (obj1.DonGia * obj1.GiamGia) / 100);
                long ClientLoginID = 0;
                if (Session["IDDN"] != null)
                    ClientLoginID = long.Parse(Session["IDDN"].ToString());
                DataTable tb = new DataTable();
                if (Session["GioHang"] != null)
                {
                    vpp.setTable((DataTable)Session["GioHang"]);
                    tb = vpp.getTable();
                    bool kt = false;
                    foreach (DataRow dr in tb.Rows)
                    {
                        if (Int64.Parse(dr[1].ToString()) == id)
                            kt = true;
                    }
                    if (kt == true)
                        tb = vpp.upDate(tb, id, 1, gia);
                    else
                        tb = vpp.addSP(id, 1, gia);
                    Session["GioHang"] = tb;
                }
                else
                {
                    tb = vpp.init(id, 1, gia);
                    Session["GioHang"] = tb;
                }

                Ultility.Alert("Đặt mua thành công!" + tb.Rows.Count, url);
            }
            catch
            {
                Ultility.Alert("Lỗi!", url);
            }
        }

        protected void drp_loai_SelectedIndexChanged(object sender, EventArgs e)
        {
            loai = int.Parse(drp_loai.SelectedValue);
            nh = int.Parse(drp_TH.SelectedValue);
            loadNews();
        }

        protected void drp_TH_SelectedIndexChanged(object sender, EventArgs e)
        {
            nh = int.Parse(drp_TH.SelectedValue);
            loai = int.Parse(drp_loai.SelectedValue);
            loadNews();
        }
    }
}
